package util;

import java.sql.*;

/*
封装此方法用于 创建链接  关闭连接
 */

public class ConnectionUtils {
    private static String username = "root";
    private static String password = "594188sbkjb";
    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/hospital_system";

    public static Connection getConn(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    //关闭连接 释放资源

    public  static void close(Connection connection , Statement statement , ResultSet ret){
        if(ret != null){
            try {
                ret.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
